import 'dart:convert';
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:json_table/json_table.dart';
import 'package:luckdragon/api/apis.dart';

var columns = [
  JsonTableColumn("pid", label: "pid"),
  JsonTableColumn("name", label: "name"),
  //JsonTableColumn("exe", label: "exe"),
  //JsonTableColumn("cwd", label: "cwd"),
  //JsonTableColumn("status", label: "status"),
  JsonTableColumn("cpu_percent", label: "cpu percent (%)"),
  JsonTableColumn("memory_percent", label: "memory percent (%)"),
];

class infoPage extends StatefulWidget {
  infoPage({Key? key, this.title = '', this.ip = '', this.port = ''})
      : super(key: key);

  final String title;
  final String ip;
  final String port;

  @override
  _infoPage createState() => _infoPage();
}

class _infoPage extends State<infoPage> {
  String json =
      '[{"pid": 0,"name": "","exe": "","cwd": "","status": "","cpu_percent": 0.0,"memory_percent": 0.0}]';

  _Load() async {
    try {
      await Apis.getProcess(widget.ip, widget.port).then((r) {
        setState(() {
          json = r.data!;
        });
      });
    } catch (e) {}
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    // setState(() {
    //   _Load();
    // });
  }

  @override
  Widget build(BuildContext context) {
    const timeout = const Duration(seconds: 4);
    Timer(timeout, () async {
      _Load();
    });
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: ListView(
          children: [
            JsonTable(jsonDecode(json), columns: columns),
          ],
        ));
  }
}

var sendShellWidget = Container(
  height: 100,
  child: Row(
    children: [Text("data")],
  ),
);
